#include "PreCompiled.h"
#include "ToolBarItem.h"
#include "Application.h"
#include "MainWindow.h"
#include "Command.h"
ToolBarItem::ToolBarItem(void)
{
}

ToolBarItem::~ToolBarItem(void)
{
	clear();
}

ToolBarItem::ToolBarItem(ToolBarItem* item)
{
	if(item)
     item->appendItem(this);
}

void ToolBarItem::setCommand(const std::string& name)
{
   _name = name;
}
std::string ToolBarItem::command()const
{
   return _name;
}

bool ToolBarItem::hasItem()const
{
   return _items.count()>0;
}
ToolBarItem* ToolBarItem::findItem(const std::string& name)
{
    if(_name == name)
		return this;
	else
	{
		for (QList<ToolBarItem*>::ConstIterator it = _items.begin(); it != _items.end(); it++)
		{
			if((*it)->_name == name)
				return *it;
		}
	}
	return 0;
}

ToolBarItem* ToolBarItem::copy()const
{
   ToolBarItem* root = new ToolBarItem;
   root->setCommand(command());

   for (QList<ToolBarItem*>::ConstIterator it = _items.begin(); it != _items.end();it++)
   {
	   root->appendItem((*it)->copy());//don't understand
   }
   return root;
}
int ToolBarItem::count()const
{
   return _items.count();
}

void ToolBarItem::appendItem(ToolBarItem*item)
{
   _items.push_back(item);
}
bool ToolBarItem::insertItem(ToolBarItem* befroe,ToolBarItem* item)
{
  int pos = _items.indexOf(befroe);
  if(pos != -1)
  {
	  _items.insert(pos,item);
	  return true;
  }else
	  return false;

}
void ToolBarItem::removeItem(ToolBarItem* item)
{
   int pos = _items.indexOf(item);
   if(pos != -1)
	   _items.removeAt(pos);
}
void ToolBarItem::clear()
{
	for(QList<ToolBarItem*>::Iterator it = _items.begin(); it != _items.end(); it++)
	{
		delete *it;
	}
	_items.clear();
}

ToolBarItem& ToolBarItem::operator << (ToolBarItem* item)
{
   appendItem(item);
   return *this;
}
ToolBarItem& ToolBarItem::operator << (const std::string& command)
{
   ToolBarItem* item = new ToolBarItem(this);
   item->setCommand(command);
   return *this;
}
QList<ToolBarItem*> ToolBarItem::getItems()const
{
	return _items;
}

// -------------------------- [4/11/2013 Administrator]
ToolBarManager* ToolBarManager::_instance = 0;
ToolBarManager::ToolBarManager()
{

}
ToolBarManager::~ToolBarManager()
{

}
ToolBarManager* ToolBarManager::getInstance()
{
	if(!_instance)
		_instance = new ToolBarManager;
	return _instance;
}
void ToolBarManager::destruct()
{
	delete _instance;
	_instance = NULL;
}

void ToolBarManager::setup(ToolBarItem* toolbaritems)
{
   if(!toolbaritems)
	   return;

   toolbarNames.clear();

   int max_width = getMainWindow()->width();
   int top_width = 0;

   QList<ToolBarItem*> items = toolbaritems->getItems();
   QList<QToolBar*>toolbars = toolBars();
   for(QList<ToolBarItem*>::ConstIterator it = items.begin(); it!= items.end(); it++)
   {
	   toolbarNames << QString::fromAscii((*it)->command().c_str());
	   std::string toolbarName = (*it)->command();
	   QToolBar* toolbar = findToolBar(toolbars,QString::fromAscii((*it)->command().c_str()));
	   bool toolbar_added = false;
	   if(!toolbar)
	   {
		   toolbar = getMainWindow()->addToolBar(QApplication::translate("Workbench",toolbarName.c_str(),0,QApplication::UnicodeUTF8));
		   toolbar->setObjectName(QString::fromAscii((*it)->command().c_str()));
		   toolbar->setVisible(true);
		   toolbar_added = true;
	   }else
	   {
		   toolbar->setVisible(true);
		   toolbar->toggleViewAction()->setVisible(true);
		   int index = toolbars.indexOf(toolbar);
		   toolbars.removeAt(index);
	   }

	   setup(*it,toolbar);

	   if(toolbar_added)
	   {
		   if(top_width >0 && getMainWindow()->toolBarBreak(toolbar))
          top_width = 0;

		   QList<QToolButton*> btns = toolbar->findChildren<QToolButton*>();
		   top_width += (btns.size() * toolbar->iconSize().width());
		   if(top_width > max_width)
		   {
			    top_width = 0;
		      getMainWindow()->insertToolBarBreak(toolbar);
		   }
	   }
   }

   //hide all unneeded toolbars

   //ignore toolbars which do not belong to the previously active workbench

    //...
}

void ToolBarManager::setup(ToolBarItem* item,QToolBar* toolbar)
{
	CommandManager& mgr = Application::Instance()->commandMananger();

	QList<ToolBarItem*> items = item->getItems();
	QList<QAction*> actions = toolbar->actions();
	for(QList<ToolBarItem*>::ConstIterator it = items.begin(); it != items.end(); it++)
	{
        QAction* action = findAction(actions,(*it)->command().c_str());
		if(!action)
		{
          if((*it)->command() == "Separator")
		  {
			  action = toolbar->addSeparator();
		  }else
		  {
			  if(mgr.addTo((*it)->command().c_str(),toolbar))
			  {
				  action = toolbar->actions().last();
			  }
			  if(action)action->setData(QString::fromAscii((*it)->command().c_str()));
		  }
		}else
		{
           int index = actions.indexOf(action);
		   actions.removeAt(index);
		}
	}

	//remove action

	for(QList<QAction*>::Iterator it = actions.begin(); it != actions.end();it++)
	{
		toolbar->removeAction(*it);
	}
}
QList<QToolBar*> ToolBarManager::toolBars()const
{
	MainWindow* mw = getMainWindow();
	QList<QToolBar*> tb;
	QList<QToolBar*> bars = mw->findChildren<QToolBar*>();
	for(QList<QToolBar*>::ConstIterator it = bars.begin(); it != bars.end(); it++)
	{
		if((*it)->parentWidget() == mw)
		tb.push_back(*it);
	}
	return tb;
}
QToolBar* ToolBarManager::findToolBar(const QList<QToolBar*>& toolbars,const QString& item)const
{
	for(QList<QToolBar*>::ConstIterator it = toolbars.begin(); it != toolbars.end(); it++)
	{
		if((*it)->objectName() == item)
           return (*it);
	}

  return NULL;
}
QAction* ToolBarManager::findAction(const QList<QAction*>& actions,const QString& item)const
{
	for(QList<QAction*>::ConstIterator it  = actions.begin(); it != actions.end(); it++)
	{
		if((*it)->data().toString() == item)
			return (*it);
	}
   return NULL;
}





